<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .right {
            color: green;
        }

        .wrong {
            color: red;
        }
    </style>
</head>

<body>
    <label for="pwd">密码：</label>
    <input type="password" class="pwd" name="pwd">&nbsp; <span>请输入密码</span> <button>点击显示密码</button>
    <script>
        var reg = /^\w{5,17}$/;
        var pwd = document.querySelector('.pwd');
        var btn = document.querySelector('button');
        // var span = document.querySelector('span');
        var flag = 0;
        btn.addEventListener('click', function () {
            if (flag == 0) {
                btn.innerHTML = '点击隐藏密码';
                pwd.type = 'text';
                flag = 1;
            } else {
                btn.innerHTML = '点击显示密码';
                pwd.type = 'password';
                flag = 0;
            }
        })
        pwd.onblur = function () {
            if (reg.test(this.value)) {
                pwd.nextElementSibling.innerHTML = '格式正确';
                pwd.nextElementSibling.className = 'right';
            } else {
                pwd.nextElementSibling.innerHTML = '格式错误';
                pwd.nextElementSibling.className = 'wrong';
            }
        }
    </script>
</body>

</html>